/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.wiki;

import org.nop.markup.tree.*;
import org.nop.templating.Template;
import org.nop.templating.XmlTreeBuilder;

/**
 *
 * @author Alexey Andreev
 */
public class BriefRenderer implements Template {
    private int maxLength = 400;
    private MarkupText markupText;
    private XmlTreeBuilder out;
    private int length = 0;

    public BriefRenderer(MarkupText markupText, int maxLength) {
        this.markupText = markupText;
        this.maxLength = maxLength;
    }

    @Override
    public void generate(XmlTreeBuilder treeBuilder) {
        this.out = treeBuilder;
        for (Block block : markupText.getBlocks()) {
            block.acceptVisitor(itemVisitor);
            if (length > maxLength) {
                break;
            }
        }
    }
    
    private BlockItemVisitor itemVisitor = new BlockItemVisitor() {
        @Override
        public void visit(Hyperlink item) {
            for (InlineSequence childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
        }
        
        @Override
        public void visit(InlineCode item) {
            out.addElement("code");
            for (InlineSequence childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
        }
        
        @Override
        public void visit(Strong item) {
            out.addElement("strong");
            for (InlineSequence childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
        }
        
        @Override
        public void visit(Emphasized item) {
            out.addElement("em");
            for (InlineSequence childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
        }
        
        @Override
        public void visit(TextContent item) {
            out.addText(item.getText());
            length += item.getText().length();
        }
        
        @Override
        public void visit(Quotation item) {
            out.addElement("p");
            for (BlockItem childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
        }
        
        @Override
        public void visit(Preformatted item) {
            out.addElement("pre");
            out.addText(item.getText());
            out.closeElement();
            length += item.getText().length();
        }
        
        @Override
        public void visit(Picture item) {
        }
        
        @Override
        public void visit(Paragraph item) {
            out.addElement("p");
            for (BlockItem childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
        }
        
        @Override
        public void visit(ListBlock item) {
            switch (item.getType()) {
                case MARKED:
                    out.addElement("ul");
                    break;
                case NUMBERED:
                    out.addElement("ol");
                    break;
            }
            outer: for (ListItem listItem : item.getItems()) {
                out.addElement("li");
                for (BlockItem child : listItem.getItems()) {
                    child.acceptVisitor(this);
                    if (length > maxLength) {
                        break outer;
                    }
                }
                out.closeElement();
            }
            out.closeElement();
        }
        
        @Override
        public void visit(Header item) {
            out.addElement("p");
            out.addElement("strong");
            for (BlockItem childItem : item.getItems()) {
                childItem.acceptVisitor(itemVisitor);
                if (length > maxLength) {
                    break;
                }
            }
            out.closeElement();
            out.closeElement();
        }
        
        @Override
        public void visit(CutBlock item) {
        }
    };
}
